<template>
	<view class="classlist">
		<view class="loadingLayout" v-if="!classList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<!-- 将id传给预览页面以获取是哪一张图 -->
			<navigator :url="'/pages/preview/preview?id='+item._id" class="item" v-for="item in classList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<view class="loadingLayout" v-if="classList.length || noData">
			<uni-load-more :status="noData?'noMore':'loading'"></uni-load-more>
		</view>
		
		<view class="safe-area-inset-bottom"></view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad, onReachBottom, onUnload } from "@dcloudio/uni-app"
import { apiGetClassList, apiGetHistoryList } from '../../api/apis';
import { onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app"
import { gotoHome } from '../../utils/common';


	const classList = ref([])
	const noData = ref(false)

	// 定义请求参数
	const queryParams = {
		pageNum:1,
		pageSize:12
	}
	
	let pageName
	
	// 生命周期获取上一页跳转过来的参数
	onLoad((e)=>{
		// console.log(e)
		// 解构出id和name
		let {id=null, name, type} = e
		if(type) {
			queryParams.type = type
		}
		if(id) {
			queryParams.classid = id
		}
		// if(!id) {
		// 	gotoHome()
		// }
		pageName = name
		// 修改导航标题
		uni.setNavigationBarTitle({
			title:name
		})
		// 执行获取分类列表功能 放在这里是因为onLoad执行的比setup晚
		getClassList()
	})
	
	// 触底加载更多数据
	onReachBottom(()=>{
		if(noData.value) {
			return
		}
		queryParams.pageNum++
		getClassList()
	})

	// 获取分类列表功能
	const getClassList = async ()=>{
		// 这里需要进行判断,看看是传id过来的还是传type过来的
		let res
		if(queryParams.classid) {
			res = await apiGetClassList(queryParams)
		}
		if(queryParams.type) {
			res = await apiGetHistoryList(queryParams)
		}
		
		classList.value = [...classList.value, ...res.data.data]
		if(queryParams.pageSize > res.data.data.length) {
			noData.value = true
		}
		uni.setStorageSync("storageClassList",classList.value)
	}

	// 分享给朋友
	onShareAppMessage((e)=>{
		// console.log(e);
		return {
			title:"咸虾米壁纸",
			path:"/pages/classlist/classlist?id="+queryParams.classid+"&name="+pageName
		}
	})
	
	// 分享给朋友圈
	onShareTimeline((e)=>{
		return {
			title:"咸虾米壁纸",
			imageUrl:"/static/xxmLogo.png",
			query:"id="+queryParams.classid+"&name="+pageName
		}
	})
	
	// 离开页面后清除缓存
	onUnload(()=>{
		uni.removeStorageSync("storageClassList")
	})
</script>

<style lang="scss" scoped>
.classlist {
	.content {
		display: grid;
		grid-template-columns: repeat(3,1fr);
		gap: 5rpx;
		padding: 5rpx;
		.item {
			height: 440rpx;
			image {
				width: 100%;
				height: 100%;
				display: block;
			}
		}
	}
}
</style>
